//
//  DABackBookTableViewCell.m
//  DABook
//
//  Created by DreamArts on 16/4/22.
//  Copyright © 2016年 lidongyang. All rights reserved.
//

#import "DABackBookTableViewCell.h"
#import "DABackBookModel.h"
#import "DALabel.h"
#import "DAHeader.h"
#import "Utility.h"
#import "DAImageView.h"
#import "UIImageView+WebCache.h"
#import "DARateView.h"
#import "DACommentView.h"
#import "DALikeAndComment.h"
#import <QuartzCore/QuartzCore.h>
#import "DACommentButton.h"

@interface DABackBookTableViewCell ()
/** 用户头像*/
@property (weak, nonatomic) IBOutlet UIImageView *userImageView;
/** 用户名字*/
@property (weak, nonatomic) IBOutlet DALabel *userNameLabel;
/** 时间轴*/
@property (weak, nonatomic) IBOutlet UILabel *timeLineLabel;
/** 时间label*/
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
/** 图书image*/
@property (weak, nonatomic) IBOutlet UIImageView *bookImageView;
/** 出借人*/
@property (weak, nonatomic) IBOutlet DALabel *lenderLabel;
/** 借阅期*/
@property (weak, nonatomic) IBOutlet DALabel *borrowTimeLabel;
/** 图书名字*/
@property (weak, nonatomic) IBOutlet DALabel *bookNameLabel;
/** 状态的图片*/
@property (weak, nonatomic) IBOutlet UIImageView *stateImageView;
/** 状态底部的背景view*/
@property (weak, nonatomic) IBOutlet UIView *stateView;
@property (weak, nonatomic) IBOutlet DALabel *rateNameLabel;
@property (weak, nonatomic) IBOutlet DALabel *EvaluateLabel;
@property (weak, nonatomic) IBOutlet DALabel *rateMessage;
@property (nonatomic, strong) NSMutableArray *heightArray;

@end
@implementation DABackBookTableViewCell

- (void)awakeFromNib {
    // Initialization code
    
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (void)setTimelineModel:(DAHomePageTimeLineModel *)timelineModel{
    if (_timelineModel != timelineModel) {
        _timelineModel = timelineModel;
    }
    self.isSelectLikeAndCommentView = YES;
    // 头像
    [self.userImageView sd_setImageWithURL:[NSURL URLWithString:timelineModel.userFace] placeholderImage:[UIImage imageNamed:@"holderface"] options:SDWebImageRefreshCached];
    
    self.userImageView.layer.cornerRadius = 3;
    self.userImageView.clipsToBounds = YES;
    // 用户名
    NSString *userName = timelineModel.userName;
    [self.userNameLabel setText:userName fitToContent:YES];
    self.userNameLabel.textColor = [UIColor colorWithHexString:bookNameColor];
    // 时间轴颜色
    self.timeLineLabel.backgroundColor = [UIColor colorWithHexString:timeLineColor];
    
    // 国际化设置
    NSString *back = NSLocalizedString(@"back", nil);
    
    // 时间
    self.timeLabel.text = [NSString stringWithFormat:@"%@ %@", self.timelineModel.timelineDate, back];
    self.timeLabel.textColor = [UIColor colorWithHexString:timeColor];
     UIView *bookCover = [[UIView alloc]initWithFrame:CGRectMake(0 , 0, self.bookImageView.frame.size.width, self.bookImageView.frame.size.height)];
        bookCover.backgroundColor = [UIColor supportGreenColor];
        DALabel *bookCoverName = [[DALabel alloc]initWithFrame:CGRectMake(5, 5, bookCover.frame.size.width -10, bookCover.frame.size.height - 10)];
        [bookCoverName setText:timelineModel.bookName fitToContent:YES];
        bookCoverName.verticalAlignment = VerticalAlignmentMiddle;
        bookCoverName.textAlignment = NSTextAlignmentCenter;
        [bookCoverName setTextColor:[UIColor whiteColor]];
        bookCoverName.font = [UIFont systemFontOfSize:12];
        [bookCover addSubview:bookCoverName];
    
    UIImage *image = [self makeImageWithView:bookCover];
    // 图书 图片
    [self.bookImageView sd_setImageWithURL:[NSURL URLWithString:timelineModel.bookImage] placeholderImage:image];
    [self.bookImageView.layer setBorderColor:[UIColor whiteColor].CGColor];
    [self.bookImageView.layer setBorderWidth:3];
    // 图书名
     NSString *bookName = timelineModel.bookName;
    [self.bookNameLabel setText:bookName fitToContent:YES];
    self.bookNameLabel.verticalAlignment = VerticalAlignmentTop;
     self.bookNameLabel.textColor = [UIColor colorWithHexString:bookNameColor];
    // 借阅期
    // 国际化设置
    NSString *borrowTime = NSLocalizedString(@"borrowTime", nil);
    NSString *str = [NSString stringWithFormat:@"%@: %@", borrowTime,timelineModel.borrowDuration];
    [self.borrowTimeLabel setText:str fitToContent:YES];
    self.borrowTimeLabel.font = [UIFont systemFontOfSize:12];
    
    // 持有人
    NSString *bookSourceStr = NSLocalizedString(@"bookSource", nil);
    NSString *bookSource = [NSString stringWithFormat:@"%@: %@", bookSourceStr,timelineModel.bookSource];
    [self.lenderLabel setText:bookSource fitToContent:YES];
    self.lenderLabel.font = [UIFont systemFontOfSize:12];
    
    // 状态图片
    self.stateImageView.backgroundColor = [UIColor colorWithHexString:@"#BFBFBF"];
    self.stateImageView.image = [UIImage imageNamed:@"book@3x"];
    self.stateImageView.layer.cornerRadius = 10;
    self.stateView.layer.cornerRadius = 10;
    self.stateView.backgroundColor = [UIColor colorWithHexString:@"#BFBFBF"];
    
    self.borrowTimeLabel.textColor = [UIColor colorWithHexString:borrowTimeColor];
    self.lenderLabel.textColor = [UIColor colorWithHexString:lenderColor];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickBookImage:)];
    [self.bookImageView addGestureRecognizer:tap];
    self.bookImageView.userInteractionEnabled = YES;
    
    // 给头像添加手势方法
    UITapGestureRecognizer *userImageTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickUserImage:)];
    self.userImageView.userInteractionEnabled = YES;
    [self.userImageView addGestureRecognizer:userImageTap];
    // 评分
    // 国际化设置
    NSString *rateStr = [NSString stringWithFormat:@"%@ :", NSLocalizedString(@"rate", nil)];
    [self.rateNameLabel setText:rateStr fitToContent:YES];
    self.rateNameLabel.textColor = [UIColor colorWithHexString:borrowTimeColor];
    
    NSInteger rate = [[NSString stringWithFormat:@"%@", timelineModel.rateNum] integerValue];
    // rateView
    [self.rateView rateNum: rate];
   // self.rateView.number = rate;
    self.rateView.backgroundColor = [UIColor colorWithHexString:backGroundColor];
    
    // 评价
    NSString *text = [NSString stringWithFormat:@"%@ :", NSLocalizedString(@"evaluateLabel", nil)];
    [self.EvaluateLabel setText:text fitToContent:YES];
    self.EvaluateLabel.textColor = [UIColor colorWithHexString:borrowTimeColor];
    
    self.rateMessage.font = [UIFont systemFontOfSize:12];
    NSString *strMessage = timelineModel.rateMessage;
    [self.rateMessage setText:strMessage fitToContent:YES];
    self.rateMessage.textColor = [UIColor colorWithHexString:borrowTimeColor];
    self.rateMessage.verticalAlignment = VerticalAlignmentTop;
    if (timelineModel.rateMessage.length == 0) {
        NSString *nothing = NSLocalizedString(@"Nothing", nil);
        [self.rateMessage setText:nothing fitToContent:YES];
        self.rateMessage.verticalAlignment = VerticalAlignmentBottom;
    }
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(isSelectChnage) name:@"isChangeSelect" object:nil];
    
    // 接受通知 刷新
    //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(DaviewLoadData:) name:@"NOTIFICATIONDATALOAD" object:nil];
    
   // self.commentView.commentArray = timelineModel.replyArray;
   // [self calculateHeight:timelineModel.replyArray];
    
//        self.commentView.sumHeight = timelineModel.rowHeight;
//        self.commentView.heightArray = timelineModel.replyHeightArray;
//        // CGRect frame = self.commentView.frame;
//        self.commentView.layer.cornerRadius = 5;
//        // frame.size.height = timelineModel.rowHeight;
//        //self.commentView.frame = frame;
//       //[self.commentView creatView];
//        [self.commentView transferCommetArray:timelineModel.replyArray indexPath:self.cellIndexPath];
    
    if (timelineModel.isReplyCommentAllShow) {
        self.commentView.sumHeight = timelineModel.rowHeight;
    }else{
        self.commentView.sumHeight = timelineModel.beforeTenRowHeight;
    }
        self.commentView.heightArray = timelineModel.replyHeightArray;
        self.commentView.isShowAllReply = timelineModel.isReplyCommentAllShow;
        self.commentView.isClickMoreDataButton = YES;
        [self.commentView transferCommetArray:timelineModel.replyArray indexPath:self.cellIndexPath];
    
    if (timelineModel.replyArray.count == 0) {
        self.commentView.backgroundColor = [UIColor colorWithHexString:backGroundColor];
    }
    
        
}
- (void)isSelectChnage{
    self.isSelectLikeAndCommentView = YES;
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"isChangeSelect" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"NOTIFICATIONDATALOAD" object:nil];
}

#pragma mark - 点击contentView 收回点赞评论
- (void)ClickContenView:(UITapGestureRecognizer *)tap{
    CATransition *animation = [CATransition animation];
    [animation setDuration:0.2];
    [animation setType:kCATransitionReveal];
    [animation setSubtype:kCATransitionFromRight];
    [animation setFillMode:kCAFillModeForwards];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    self.likeAndCommentView.frame = CGRectZero;
    self.likeAndCommentView.hidden = YES;
    [self.likeAndCommentView.layer addAnimation:animation forKey:nil];
    self.isSelectLikeAndCommentView = YES;
}
- (void)setCellIndexPath:(NSIndexPath *)cellIndexPath{
    if (_cellIndexPath != cellIndexPath) {
        _cellIndexPath = cellIndexPath;
    }
}
#pragma mark - 点击头像触发的方法
- (void)clickUserImage:(UITapGestureRecognizer *)tap{
    NSLog(@"back  点了头像");
    [self.delegate backCellTurnPersonalPageWithUserID:self.timelineModel.userID];
}
#pragma mark - 手势方法
- (void)clickBookImage:(UITapGestureRecognizer *)tap{
    NSLog(@"点击了图书");
    [self.delegate showBookDetail:self.timelineModel.bookID];
}
#pragma mark - 计算文本高度
- (CGFloat)heightWithText:(NSString *)text Width:(CGFloat)width Font:(CGFloat)font{
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:font]};
    CGSize size = CGSizeMake(width, 200000);
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    /** 返回得到的高度 */
    return rect.size.height;
}




@end
